let app = getApp();
Page({
  data: {
    types: ["问大家", '晒商品', '咨询店家', '差评', '好评', '其他'],
    type: '请选择问题类型',
    imgList: [],
    fileIDs: [],
  },
  //选择商品类型
  bindPickerChange(e) {
    this.setData({
      type: this.data.types[e.detail.value]
    })
  },

  //选择图片
  ChooseImage() {
    wx.chooseImage({
      count: 8 - this.data.imgList.length, //默认9,我们这里最多选择8张
      sizeType: ['compressed'], //可以指定是原图还是压缩图，这里用压缩
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        console.log("选择图片成功", res)
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        console.log("路径", this.data.imgList)
      }
    });
  },
  //删除图片
  DeleteImg(e) {
    wx.showModal({
      title: '要删除这张照片吗？',
      content: '',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })


  },

  //上传数据
  publish(e) {
    let uploadedCount = 0;
    let totalFiles = this.data.imgList.length;

    let good = e.detail.value
    if (good.type == '请选择问题类型') {
      wx.showToast({
        icon: "error",
        title: '请选择问题类型'
      })
      return
    }
    if (!good.content || good.content.length < 6) {
      wx.showToast({
        icon: "none",
        title: '描述必须大于6个字'
      })
      return
    }
    //图片相关
    let imgList = this.data.imgList
    if (!imgList || imgList.length < 1) {
      wx.showToast({
        icon: "none",
        title: '请选择图片'
      })
      return
    }
    wx.showLoading({
      title: '发布中...',
    })

    const promiseArr = []
    //只能一张张上传 遍历临时的图片数组
    for (let i = 0; i < this.data.imgList.length; i++) {
      let filePath = this.data.imgList[i]
      wx.uploadFile({
        filePath: filePath,
        name: 'file',
        url: 'http://localhost:8080/api/image/forum/upload',
        method:'POST',
        header: {
          "content-type": "multipart/form-data"
        },
        success: (res) => {
          console.log("上传成功", res.data);
          this.data.fileIDs[i] = res.data
          console.log(this.data.fileIDs);
          this.data.fileIDs.push(res.data)
          uploadedCount++
          if (uploadedCount == totalFiles) {
            wx.request({
              url: 'http://localhost:8080/api/forum/add',
              method: 'POST',
              data: {
                content: good.content,
                type: good.type, //类型
                imgs: JSON.stringify(this.data.fileIDs),
              },
              success: (res) => {
                if (res.data.code == 200) {
                  //清空数据
                  this.setData({
                    imgList: [],
                    fileIDs: [],
                  })
                  wx.hideLoading()
                  wx.showToast({
                    title: '发布成功',
                  })
                  setTimeout(() => {
                    wx.switchTab({
                      url: '/pages/forum/forum',
                    })
                  }, 2000)
                } else {
                  wx.hideLoading()
                  wx.showToast({
                    icon: 'none',
                    title: res.data.msg
                  })
                }
              },
              fail: (res) => {
                wx.hideLoading()
                wx.showToast({
                  icon: 'none',
                  title: '网络不给力....'
                })
                console.error('发布失败', res)
              }
            })
          }
        }
      })
    }

  },
})